package gash.ejb.echo;

import java.util.Random;
import java.util.UUID;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

/**
 * Example of an EJB3.1 using an interface to define functional behavior. Note
 * that we no longer have to define the remote and local interfaces as separate
 * .java files
 * 
 * @author gash
 * 
 */
@Stateless
@Remote(EchoPoint.class)
public class EchoPointEJB implements EchoPoint {
	private static final long serialVersionUID = 308915085712926017L;
	private int count;
	private String uid = UUID.randomUUID().toString();

	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public String echo(String what) {
		try {
			Random ran = new Random();
			count++;
			System.out.println("Echo (" + uid + "): " + what);
			Thread.sleep(30 * (ran.nextInt(10) + 1));
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return what;
	}

	@AroundInvoke
	public Object preCheck(InvocationContext ctx) throws Exception {
		System.out.println("Echo's Preflight check!");

		return ctx.proceed();
	}
}
